package com.lovestudying.controller;

import java.io.IOException;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.lovestudying.entity.Comment;
import com.lovestudying.service.CommentService;
import com.lovestudying.service.LikeListService;
import com.lovestudying.service.impl.CommentServiceImpl;
import com.lovestudying.service.impl.LikeListServiceImpl;
import com.lovestudying.util.DBUtil_Pro;
import com.lovestudying.util.PageInfo;

/**
 * 评论(comment)Controller
 * 
 * @author OldDeer
 *
 */
@WebServlet("/CommentServlet")
public class CommentServlet extends HttpServlet {
	Gson gson = new Gson();
	CommentService commentService = new CommentServiceImpl();
	LikeListService likeListService = new LikeListServiceImpl();
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CommentServlet() {

		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String parameter = request.getParameter("op");
		switch (parameter) {
		case "GetCommentsByUUID":
			GetCommentsByUUID(request, response);
			break;
		case "PostComment":
			PostComment(request, response);
			break;
		case "deleteComOrRep":
			deleteComOrRep(request, response);
			break;

		default:
			break;
		}
		// super.service(request, response);
	}

	private void deleteComOrRep(HttpServletRequest request, HttpServletResponse response) throws IOException {
		// TODO 自动生成的方法存根
		String deleteId = request.getParameter("deleteId");
		String deleteComOrRepById = commentService.deleteComOrRepById(deleteId);
		response.getWriter().print(deleteComOrRepById);

	}

	/**
	 * 发表评论
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void PostComment(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String CommentUuid = DBUtil_Pro.GetUUID();
		String CreateTime = DBUtil_Pro.GetNowTime();
		String AccountId = request.getParameter("AccountId");
		String CommentCotent = request.getParameter("Cotent");
		String comment_gruop = request.getParameter("comment_gruop");
		if (commentService.PostComment(CommentUuid, CreateTime, AccountId, CommentCotent, comment_gruop)) {
			response.getWriter().print("评论成功");
		} else {
			response.getWriter().print("评论失败");
		}
	}

	/**
	 * 获取一个对象下面的评论组
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void GetCommentsByUUID(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String Uuid = request.getParameter("UUID");
		String orderParam = request.getParameter("orderParam");
		String desc = request.getParameter("desc");
		Integer pageSize = null;
		Integer pageNum = null;
		String NowAccount = request.getParameter("NowAccount");
		if (request.getParameter("pageSize") != null) {
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		if (request.getParameter("pageNum") != null) {
			pageNum = Integer.parseInt(request.getParameter("pageNum"));
		}
		// 获得查询到的一级评论
		PageInfo<Comment> getCommentsByUUID = commentService.GetCommentsByUUID(Uuid, orderParam, desc, pageSize,
				pageNum);
		// 根据每条评论查找到当前账户是否点赞
		for (Comment element : getCommentsByUUID.getData()) {
			element.setThisAccountLike(likeListService.getCommentLikeState(NowAccount, element.getComment_id()));
		}
		String JgetCommentsByUUID = gson.toJson(getCommentsByUUID);
		response.getWriter().print(JgetCommentsByUUID);

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// System.out.println(request.getParameter("op") + "get");
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// System.out.println(request.getParameter("op") + "post");
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
